package com.atguigu.ssyx.home.controller;


import org.junit.Test;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class Demo1 {


    public static void main1(String[] args) {
        //1.创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        System.out.println("开始");

        CompletableFuture<Void> reuslt = CompletableFuture.runAsync(() -> {
            System.out.println("当前线程:" + Thread.currentThread().getName());
            System.out.println("执行任务1");
        }, executorService);
        System.out.println("结束");
    }

    public static void main2(String[] args) throws Exception {
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        System.out.println("main begin....");
        CompletableFuture<Integer> completableFuture =
                CompletableFuture.supplyAsync(() -> {
                    System.out.println("当前线程：" + Thread.currentThread().getId());
                    int result = 1024;
                    System.out.println("result:" + result);
                    return result;
                }, executorService);
        //获取返回结果
        Integer value = completableFuture.get();
        System.out.println("main over...." + value);
    }

    public static void main3(String[] args) throws Exception {
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        System.out.println("main begin....");
        CompletableFuture<Integer> completableFuture =
                CompletableFuture.supplyAsync(() -> {
                            System.out.println("当前线程：" + Thread.currentThread().getId());
                            int result = 10 / 0;
                            System.out.println("result:" + result);
                            return result;
                        }, executorService)
                        .whenComplete((rs, exception) -> {
                            System.out.println("结果：" + rs);
                            System.out.println(exception);
                        });
        System.out.println("main over....");
    }

    public static void main4(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        //线程1执行返回的结果是:100
        CompletableFuture<Integer> futureA = CompletableFuture.supplyAsync(() -> {

            System.out.println("线程1：" + Thread.currentThread().getId());
            return 100;
        }, executorService).thenApply(rs -> {
            //这里的返回值是100
            System.out.println("这里的返回值是" + rs);
            return rs;
        });


        //线程2拿到线程1的结果
        CompletableFuture<Integer> futureB = futureA.thenApplyAsync((rs) -> {

            System.out.println("线程2：" + Thread.currentThread().getId());
            System.out.println(Thread.currentThread().getId() + "线程2拿到线程1的结果是:" + rs);
            return rs;
        }, executorService);


        //线程3无法获取线程1的结果
        CompletableFuture<Void> futureC = futureA.thenRunAsync(() -> {

            System.out.println("线程3：" + Thread.currentThread().getId());
            System.out.println(Thread.currentThread().getId() + "线程3无法获取线程1的结果");
        }, executorService);
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(4);
        //线程1执行返回的结果是:100
        CompletableFuture<Integer> futureA = CompletableFuture.supplyAsync(() -> {

            System.out.println(Thread.currentThread().getName() + "--begin..");

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName() + "--over..");
            return 100;
        }, executorService);


        //线程2执行返回的结果是:100
        CompletableFuture<Integer> futureB = CompletableFuture.supplyAsync(() -> {

            System.out.println(Thread.currentThread().getName() + "--begin..");
            System.out.println(Thread.currentThread().getName() + "--over..");
            return 100;
        }, executorService);

        //等待两个线程都执行完成 在执行下面的代码
        CompletableFuture<Void> ALL = CompletableFuture.allOf(futureA, futureB);
        ALL.get();
        System.out.println("all over...");


    }
}